package com.weetop.akly.activity.meessage;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.weetop.akly.R;
import com.weetop.akly.base.BaseFragment;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.utils.Tablayout.SegmentTabLayout;
import com.weetop.akly.utils.Tablayout.listener.OnTabSelectListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;

public class MsgMainFragment extends BaseFragment {
    @BindView(R.id.tl_1)
    SegmentTabLayout tl1;
    Unbinder unbinder;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private String[] mTitles = {"消息", "私信"};
    private MessageFragment messageFragment;
    ConversationListFragment sixin;
    private Fragment[] fragments;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_msg_main, null);
        unbinder = ButterKnife.bind(this, view);
        tl1.setTabData(mTitles);
        messageFragment = new MessageFragment();

        sixin = new ConversationListFragment();
//        Uri sixinuri = Uri.parse("rong://com.meetba.baotuan.dongtai").buildUpon()
//                .appendPath("conversationlist")
//                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")
//                .build();
        Uri sixinuri = Uri.parse("rong://" +
                getActivity().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//群组
                .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
                .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系统
                .build();
        sixin.setUri(sixinuri);


        fragments = new Fragment[]{messageFragment, sixin};

        tl1.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchfragment(fragments[position]);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        switchfragment(messageFragment);
        return view;
    }
    /**
     * 选择fragment
     *
     * @param fragment
     */
    private void switchfragment(Fragment fragment) {
        // 设置一个默认值
        if (fragment == null) {
            fragment = messageFragment;
        }
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment1 : fragments) {
            if (fragment1 != fragment) {
                if (fragment1 != null && !fragment1.isHidden() && fragment1.isAdded()) {
                    ft.hide(fragment1);
                }
            }
        }

        if (fragment.isAdded() && fragment.isHidden()) {
            ft.show(fragment);
        } else {
            if (!fragment.isAdded()) {
                ft.add(R.id.frame_msg, fragment);
            }
        }
        ft.commitAllowingStateLoss();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
